home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / ftpsubr.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  5KB  |  229 lines

  1. /****************************************************************************
  2. *    $Id: ftpsubr.c 1.4 94/02/02 14:38:13 ROOT_DOS Exp $
  3. *    22 Dec 93    1.2        GT    Fix "hash" output.                                
  4. *    24 Jan 93    1.3        GT    Fix hash mark wrap column.                        
  5. *    01 Feb 94    1.4        GT    Remove broken wrap code.
  6. *
  7. *  Atari Version by David Nash - dnash@chaos.demon.co.uk
  8. *
  9. *  Add V_BYTE verbosity level
  10. *
  11. ****************************************************************************/
  12.  
  13. /* Routines common to both the FTP client and server
  14.  * Copyright 1991 Phil Karn, KA9Q
  15.  */
  16. #include <stdio.h>
  17. #include "global.h"
  18. #include "mbuf.h"
  19. #include "socket.h"
  20. #include "proc.h"
  21. #include "ftp.h"
  22. #include "ftpcli.h"
  23.  
  24. extern int16 Tcp_mss;
  25.  
  26. static void hashit __ARGS((int hash, long total, long *hmark, int *col,
  27.                            int wrapcol));
  28.  
  29. /* Send a file (opened by caller) on a network socket.
  30.  * Normal return: count of bytes sent
  31.  * Error return: -1
  32.  */
  33. long
  34. sendfile(fp,s,mode,hash, wrapcol)
  35. FILE *fp;    /* File to be sent */
  36. int s;        /* Socket to be sent on */
  37. int mode;    /* Transfer mode */
  38. int hash;    /* Print hash marks every BLKSIZE bytes */
  39. int wrapcol;    /* wrap column when hashing            */
  40. {
  41.     register struct mbuf *bp;
  42.     int c,oldf;
  43.     long total = 0;
  44.     long hmark = 0;
  45.     int col = 0;
  46.  
  47.      if (hash == V_BYTE)
  48.          tprintf("         : Bytes Sent\r");
  49.  
  50.     switch(mode){
  51.     default:
  52.     case LOGICAL_TYPE:
  53.     case IMAGE_TYPE:
  54.         sockmode(s,SOCK_BINARY);
  55.         for(;;){
  56.             bp = ambufw(BLKSIZE);
  57.             if((bp->cnt = fread(bp->data,1,BLKSIZE,fp)) == 0){
  58.                 free_p(bp);
  59.                 break;
  60.             }
  61.             total += bp->cnt;
  62.             if(send_mbuf(s,bp,0,NULLCHAR,0) == -1){
  63.                 total = -1;
  64.                 break;
  65.             }
  66.  
  67.             hashit (hash, total, &hmark, &col, wrapcol);
  68.         }
  69.         break;
  70.     case ASCII_TYPE:
  71.         oldf = setflush(s,-1);
  72.         /* Let the newline mapping code in usputc() do the work */
  73.         sockmode(s,SOCK_ASCII);
  74.         while((c = getc(fp)) != EOF){
  75. #if !defined(UNIX) && !defined(__TURBOC__)
  76.             if(c == '\r'){
  77.                 /* Needed only if the OS uses a CR/LF
  78.                  * convention and getc doesn't do
  79.                  * an automatic translation
  80.                  */
  81.                 continue;
  82.             }
  83. #endif
  84.             if(usputc(s,(char)c) == -1){
  85.                 total = -1;
  86.                 break;
  87.             }
  88.             total++;
  89.             hashit (hash, total, &hmark, &col, wrapcol);
  90.         }
  91.         usflush(s);
  92.         setflush(s,oldf);        
  93.         break;
  94.     }
  95.     if(hash)
  96.         tputc('\n');
  97.     return total;
  98. }
  99. /* Receive a file (opened by caller) from a network socket.
  100.  * Normal return: count of bytes received
  101.  * Error return: -1
  102.  */
  103. long
  104. recvfile(fp,s,mode,hash, wrapcol)
  105. FILE *fp;
  106. int s;
  107. int mode;
  108. int hash;
  109. int wrapcol;
  110. {
  111.     int cnt,c;
  112.     struct mbuf *bp;
  113.     long total = 0;
  114.     long hmark = 0;
  115.     int col = 0;
  116.  
  117.     if (hash == V_BYTE)
  118.         tprintf("         : Bytes Received\r");
  119.  
  120.     switch(mode){
  121.     default:
  122.     case LOGICAL_TYPE:
  123.     case IMAGE_TYPE:
  124.         sockmode(s,SOCK_BINARY);
  125.         while((cnt = recv_mbuf(s,&bp,0,NULLCHAR,0)) != 0){
  126.             if(cnt == -1){
  127.                 total = -1;
  128.                 break;
  129.             }
  130.             total += cnt;
  131.             hashit (hash, total, &hmark, &col, wrapcol);
  132.             if(fp != NULLFILE){
  133.                 if(write_p(fp,bp) == -1){
  134.                     free_p(bp);
  135.                     total = -1;
  136.                     break;
  137.                 }
  138.                 free_p(bp);
  139.             } else {
  140.                 send_mbuf(Curproc->output, bp, 0, NULLCHAR, 0);
  141.             }
  142.         }
  143.         break;
  144.     case ASCII_TYPE:
  145.         sockmode(s,SOCK_ASCII);
  146.         while((c = recvchar(s)) != EOF){
  147.             if(fp != NULLFILE){
  148. #if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
  149.                 if(c == '\n'){
  150.                     /* Needed only if the OS uses a CR/LF
  151.                      * convention and putc doesn't do
  152.                      * an automatic translation
  153.                      */
  154.                     putc('\r',fp);
  155.                 }
  156. #endif
  157.                 if(putc(c,fp) == EOF){
  158.                     total = -1;
  159.                     break;
  160.                 }
  161.             } else {
  162.                 tputc((char)c);
  163.             }
  164.             total++;
  165.             hashit (hash, total, &hmark, &col, wrapcol);
  166.         }
  167.         /* Detect an abnormal close */
  168.         if(socklen(s,0) == -1)
  169.             total = -1;
  170.         break;
  171.     }
  172.     if(hash)
  173.         tputc('\n');
  174.     return total;
  175. }
  176.  
  177. /****************************************************************************
  178. *    Print hash mark if required.                                            *
  179. ****************************************************************************/
  180.  
  181. static void hashit(int hash, long total, long *hmark, int *col, int wrapcol)
  182. {
  183.     while (hash == V_BYTE && total >= *hmark + 1024) {
  184.         tprintf("%8ld\r", total);
  185.         *hmark += 1024;
  186.  
  187.         return;
  188.     }
  189.     
  190.     while (hash && total >= *hmark + 1024) {
  191.         tputc ('#');
  192.         *hmark += 1024;
  193.  
  194.         if (wrapcol != 0 && ++(*col) >= wrapcol) {
  195.             *col = 0;
  196.             tputc ('\n');
  197.         }                
  198.     }
  199. }
  200.  
  201.  
  202. /* Determine if a file appears to be binary (i.e., non-text).
  203.  * Return 1 if binary, 0 if ascii text after rewinding the file pointer.
  204.  *
  205.  * Used by FTP to warn users when transferring a binary file in text mode.
  206.  */
  207. int
  208. isbinary(fp)
  209. FILE *fp;
  210. {
  211.     int c,i;
  212.     int rval;
  213.  
  214.     rval = 0;
  215.     for(i=0;i<512;i++){
  216.         if((c = getc(fp)) == EOF)
  217.             break;
  218.         if(c & 0x80){
  219.             /* High bit is set, probably not text */
  220.             rval = 1;
  221.             break;
  222.         }
  223.     }
  224.     /* Assume it was at beginning */
  225.     fseek(fp,0L,SEEK_SET);
  226.     return rval;
  227. }
  228.  
  229.